package com.smap.control;

import com.smap.core.XY;
import com.smap.event.MapEvent;
import com.smap.layer.VectorLayer;
import com.smap.vector.Feature;
import com.smap.vector.LineString;
import com.smap.vector.Point;
import com.smap.vector.Polygon;
import com.smap.vector.Style;

public class VectorControl extends AbstractControl {
	// for activeTool ,select point line polygon
	Feature selectedFeat;
	Feature newFeat;
	VectorLayer layer;

	public VectorControl() {

	}

	public VectorControl(VectorLayer layer) {
		this();
		this.layer = layer;
	}

	@Override
	void onEvent(MapEvent evt) {
		boolean redraw = false;
		if ("draging".equals(evt.type)) {
			// System.out.println(evt);
		} else if ("click".equals(evt.type)) {
			XY click = new XY(evt.x, evt.y);
			XY clickCoord = map.pixelToMap(click);

			if ("select".equals(activeTool)) {
				newFeat = null;
				selectedFeat = layer.select(click);
				// todo, maybe need a unselected event
				map.fireEvent(new MapEvent(MapEvent.EVT_FEAT_SELECTED,
						selectedFeat));
			} else if ("point".equals(activeTool)) {
				newFeat = new Feature(new Point(clickCoord), Style.NEW_FEATURE);
				try {
					layer.add(newFeat);
				} catch (Exception e) {
					e.printStackTrace();
				}
				map.fireEvent(new MapEvent(MapEvent.EVT_FEAT_ADDED, newFeat));
			} else if ("line".equals(activeTool)) {
				if (newFeat == null) {
					newFeat = new Feature(new LineString(), Style.NEW_FEATURE);
					newFeat.status = 1;
					layer.add(newFeat);
				}
				LineString line = (LineString) newFeat.geom;
				line.add(clickCoord);
			} else if ("polygon".equals(activeTool)) {
				if (newFeat == null) {
					newFeat = new Feature(new Polygon(), Style.NEW_FEATURE);
					newFeat.status = 1;
					layer.add(newFeat);
				}
				Polygon polygon = (Polygon) newFeat.geom;
				polygon.add(clickCoord);
			}
			redraw = true;
			if (newFeat != null) {
				newFeat.createScreen(map, true);
			}
		} else if ("dbclick".equals(evt.type)) {
			evt.stop();
			if (newFeat != null) {
				if ("line".equals(activeTool)) {// finish line
					newFeat.status = 0;
					newFeat = null;
					redraw = true;
				} else if ("polygon".equals(activeTool)) {// finish polygon
					newFeat.status = 0;
					newFeat = null;
					redraw = true;
				}
			}
		} else if (MapEvent.EVT_MAP_DRAWN.equals(evt.type)) {
			redraw = true;
		}
		if (redraw) {
			layer.draw();
			draw();
		}
	}

	private void draw() {
		if (selectedFeat != null) {
			Style style = selectedFeat.style.clone();
			style.stroke_width++;
			style.line_width++;
			layer.draw(selectedFeat, style);
			style.label_color = "blue";
			layer.drawLabel(selectedFeat, style);
		}
	}

	public void setLayer(VectorLayer layer) {
		this.layer = layer;
	}

	@Override
	public void execute(String cmd) {
		if (newFeat != null) {
			newFeat.status = 0;
			newFeat = null;
		}
		System.out.println(this.activeTool);
		/*
		 * String changeTo = null; if ("select".equals(cmd) ||
		 * "point".equals(cmd) || "line".equals(cmd) || "polygon".equals(cmd)) {
		 * changeTo = cmd; } System.out.println(changeTo); if (changeTo != null
		 * && !changeTo.equals(activeTool)) { System.out.println(changeTo);
		 * this.activate(changeTo); } else { this.deActivate(); }
		 */
	}

	@Override
	public String[] getCmd() {
		return new String[] { "select", "point", "line", "polygon" };
	}

	@Override
	void init() {
		this.setGroup("main");
	}

}
